# language: JSVM2

/**
 * @fileoverview js.util.Stack class {@link http://jsvm.org/}
 * @file		Stack.jsc
 * @author	Wan Changhua
 * @version	2.01, 10/23/05
 * @since		JSVM2.0
 */

package js.util;

import js.lang.Cloneable;
import js.util.ArrayList;

/**
 * Creates an empty Stack.
 * Inherit from Cloneable
 * @author	Wan Changhua
 * @version	2.01, 10/23/05
 * @extends Cloneable
 * @class This is the stack class.
 * @constructor
 */
class Stack extends Cloneable () {

	/**
	 * @private
	 */
	this.__list = new js.util.ArrayList();
}

var $p = Stack.prototype;

/**
 * Tests if this stack is empty.
 * @return  <code>true</code> if and only if this stack contains 
 *          no items; <code>false</code> otherwise.
 * @type boolean
 */
$p.empty = function() {
	return (this.__list.size() == 0);
}

/**
 * Looks at the object at the top of this stack without removing it 
 * from the stack. 
 *
 * @return     the object at the top of this stack (the last item 
 *             of the <tt>Vector</tt> object). 
 * @type Object
 */
$p.peek = function() {
	if (this.empty()) {
		return null;
	}
	var o = this.__list.get(0);
	return o;
}

 /**
  * Removes the object at the top of this stack and returns that 
  * object as the value of this function. 
  *
  * @return     The object at the top of this stack (the last item 
  *             of the <tt>Vector</tt> object).
  * @exception  EmptyStackException  if this stack is empty.
  * @type Object
  */
$p.pop = function() {
	if (this.empty()) {
		return null;
	}
	var o = this.__list.get(0);
	this.__list.removeAt(0);
	return o;

}


/**
 * Pushes an item onto the top of this stack. This has exactly 
 * the same effect as:
 * <blockquote><pre>
 * addElement(item)</pre></blockquote>
 *
 * @param   item   the item to be pushed onto this stack.
 * @return  the <code>item</code> argument.
 * @type boolean
 */
$p.push = function(o) {
	this.__list.add(0, o);
}


/**
 * Returns the 1-based position where an object is on this stack. 
 * If the object <tt>o</tt> occurs as an item in this stack, this 
 * method returns the distance from the top of the stack of the 
 * occurrence nearest the top of the stack; the topmost item on the 
 * stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt> 
 * method is used to compare <tt>o</tt> to the 
 * items in this stack.
 *
 * @param   o   the desired object.
 * @return  the 1-based position from the top of the stack where 
 *          the object is located; the return value <code>-1</code>
 *          indicates that the object is not on the stack.
 * @type Object
 */
$p.search = function(o) {
	return this.__list.indexOf(o);
}

/**
 * Returns a shallow copy of this <tt>Stack</tt> instance.  (The
 * elements themselves are not copied.)
 *
 * @return  a clone of this <tt>Stack</tt> instance.
 * @type Stack
 */
$p.clone = function() {
	var o = new Stack();
	o.__list = this.__list.clone();
	return o;
}
